/**
 * This file is a part of qloudgen-rul. 
 * You can redistribute qloudgen-rul and/or modify it under the terms of the Lesser GNU General Public License version 3. 
 * qloudgen-rul is distributed WITHOUT ANY WARRANTY. 
 *
 * See the Lesser GNU General Public License for more details at http://www.gnu.org/licenses/. 
 **/
package org.qloudgen.rul.curator;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.apache.curator.utils.ZKPaths;
import org.apache.curator.utils.ZKPaths.PathAndNode;

import org.qloudgen.sec.util.ExceptionUtil;

/**
 * <p>Title: Curator Framework Wrapper</p>
 * <p>Description: Wrapper curator framework recipes cache implementation.</p>
 * <p>Copyright: qloudgen/rul 2014</p>
 * @author <a href="mailto:hyq.dd.sun@gmail.com">glue.3M</a>
 */
public abstract class AbstractPathCacheClient extends AbstractCuratorClient implements CuratorPathCache {
	/**
	 * Log output.
	 */
	private final static Logger logger = LoggerFactory.getLogger( AbstractPathCacheClient.class );
	/**
	 *
	 */
	private final static String DEFAULT_PATH_ROOT = "/repository";
	/**
	 *
	 */
	private String root = null;
	
	/**
	 * Default constructor.
	 */
	public AbstractPathCacheClient() {
		// super().
		this.root = DEFAULT_PATH_ROOT;
	}
	
	/**
	 * Custom constructor.
	 *
	 * @param root
	 * @param configFile
	 */
	public AbstractPathCacheClient(String root , String configFile) {
		//
		super( configFile );
		//
		this.root = root;
	}
	
	/**
	 * Sets current root.
	 *
	 * @param root
	 */
	public void setRoot(final String root) {
		this.root = root;
	}
	
	/**
	 * Returns current root.
	 *
	 * @return String
	 */
	public String getRoot() {
		return root;
	}
	
	/**
	 * Returns the String representation.
	 *
	 * @return String
	 */
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append( "[" );
		builder.append( super.toString() );
		builder.append( "-->" );
		builder.append( "root=" ).append( root );
		builder.append( "]" );
		//
		return builder.toString();
	}
	
	/**
	 * Returns current path.
	 *
	 * @param groupId
	 * @param artifactId
	 * @param version
	 * @return String
	 */
	public String getPathFromReleaseId(String groupId , String artifactId , String version) {
		String path = ZKPaths.makePath( getRoot() , groupId + ":" + artifactId + ":" + version );
		logger.trace( "Current repository cache path is...{}" , path );
		return path;
	}
	
	/**
	 * Returns current release id.
	 *
	 * @param path
	 * @return String
	 */
	public String getReleaseIdFromPath(String path) {
		String releaseId = ZKPaths.getNodeFromPath( path );
		logger.trace( "Current repository cache release id is...{}" , releaseId );
		return releaseId;
	}
	
	/**
	 * Check child.
	 *
	 * @return boolean
	 */
	public boolean isExist(String path) {
		try {
			return ( getClient().checkExists().forPath( path ) == null ? false : true );
		}
		catch (Throwable t) {
			logger.error( "Current repository cache check child error...\n{}" , ExceptionUtil.get( t ) );
			throw new CuratorException( t );
		}
	}
	
	/**
	 * Returns current path.
	 *
	 * @param groupId
	 * @param artifactId
	 * @param version
	 * @return String
	 */
	public String getPathFromReleaseId2(String groupId , String artifactId , String version) {
		String path = ZKPaths.makePath( ZKPaths.makePath( ZKPaths.makePath( getRoot() , groupId.replaceAll( "\\." , "/" ) ) , artifactId ) , version );
		logger.trace( "Current repository cache path is...{}" , path );
		return path;
	}
	
	/**
	 * Returns current release id.
	 *
	 * @param path
	 * @return String
	 */
	public String getReleaseIdFromPath2(String path) {
		String groupId = "";
		String artifactId = "";
		String version = "";
		PathAndNode node1 = ZKPaths.getPathAndNode( path.replaceFirst( getRoot() , "" ) );
		version = node1.getNode();
		if ( ! node1.getPath().equals( "/" ) ) {
			PathAndNode node2 = ZKPaths.getPathAndNode( node1.getPath() );
			artifactId = node2.getNode();
			if ( ! node2.getPath().equals( "/" ) ) {
				PathAndNode node3 = ZKPaths.getPathAndNode( node2.getPath() );
				if ( ! node3.getPath().equals( "/" ) ) {
					groupId = node3.getPath().replaceFirst( "/" , "" ).replaceAll( "/" , "." );
					groupId = groupId + "." + node3.getNode();
				}
				else {
					groupId = node3.getNode();
				}
			}
		}
		String releaseId = groupId + ":" + artifactId + ":" + version;
		logger.trace( "Current repository cache release id is...{}" , releaseId );
		return releaseId;
	}
}